package com.A21.GuoSai.demo.service_entity.utils;

import io.camunda.zeebe.model.bpmn.BpmnModelInstance;
import io.camunda.zeebe.model.bpmn.instance.ExtensionElements;
import io.camunda.zeebe.model.bpmn.instance.Task;
import io.camunda.zeebe.model.bpmn.instance.zeebe.ZeebeInput;
import io.camunda.zeebe.model.bpmn.instance.zeebe.ZeebeIoMapping;
import org.springframework.stereotype.Service;

import java.util.Collection;

@Service
public class BpmnService {


    //将bpmn文件中的变量写入数据库
    public void writeVariblesFromBpmn(String bpmnStr){
        BpmnModelInstance modelInstance=BpmnUtils.getModelInstance(bpmnStr);
        Collection<Task> modelElementsByType = modelInstance.getModelElementsByType(Task.class);
        for(Task task:modelElementsByType){
            Collection<ExtensionElements> extensionElements = task.getChildElementsByType(ExtensionElements.class);
            for (ExtensionElements extensionElement:extensionElements) {
                Collection<ZeebeIoMapping> childElementsByType = extensionElement.getChildElementsByType(ZeebeIoMapping.class);
                for(ZeebeIoMapping ioMap:childElementsByType){
                    //获取所有的输入input
                    Collection<ZeebeInput> inputs = ioMap.getInputs();
                    for(ZeebeInput input:inputs){
                        /*这里获取到了bpmn图中的input变量值，getTarget()获取的是变量名getSource()获取的是变量的值*/
                        System.out.println(input.getTarget()+"="+input.getSource());
                    }
                }
            }
        }
    }
}
